Skip to content

feat: accept C++ integers as a key in operator[] - #6291

Merged
henryiii merged 2 commits into
pybind:masterfrom
henryiii:feat/item-accessor-int-key
Nov 23, 2026
Merged

henryiii merged 2 commits into
pybind:masterfrom
henryiii:feat/item-accessor-int-key

Conversation

@henryiii

@henryiii henryiii commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

🤖 AI text below 🤖

Description

Closes #1309. Alternative to, and closes #6286 that follows the review there: only integer keys are accepted, and other key types still need an explicit py::cast(key).

Closes #6286.

obj[key] now accepts a C++ integer, for example d[1], d[-1], d[std::size_t{2}] and o[1][2]. The key is built with py::int_.

The change adds one constrained template overload of object_api::operator[]. The handle, object && and const char * overloads are unchanged.

bool and character types are excluded. d['a'] would otherwise become the key 97, while py::cast gives a str.

py::tuple, py::list and py::sequence declare their own operator[], which hides the new overload. t[1] on those types still uses the size_t accessors.

Behaviour change: obj[0] compiled before, because the literal 0 converted to a null const char * and went to py::str(nullptr). It is now the integer key 0.

is_std_char_type moves from cast.h to detail/common.h so that pytypes.h can use it.

Suggested changelog entry:

  • operator[] on py::object, py::dict and accessors now accepts a C++ integer as the key (for example d[1]). The literal 0 is now the integer key 0; before, it was a null const char *.

@henryiii
henryiii marked this pull request as ready for review November 22, 2026 04:02
Assisted-by: ClaudeCode:claude-fable-5-2
@henryiii
henryiii force-pushed the feat/item-accessor-int-key branch from 15c76db to e16a4f7 Compare November 22, 2026 04:43
@rwgk

rwgk commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

I added Closes #6286 to the PR description. I'm much more comfortable with this alternative.

@rwgk

rwgk commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

codex GPT-6.2-Sol ultra (I'll add a commit with the suggested changes):

Tests look good, one edge case before approval:

The tests cover reads and writes with zero, negative integers, and size_t, plus chained indexing. The new binding fails to compile against the base revision, and all 138 local pytypes tests passed on both CPython builds. One optional improvement is to declare py::dict d; so the fixture directly exercises the typed dictionary wrapper. Test fixture · Passing CI

I’d ask for a small width guard before merging. In GNU C++20 mode with libstdc++, std::is_integral accepts unsigned __int128, and py::int_ narrows it to unsigned long long. I verified that assigning to key 2**100 silently overwrites key 0. A scratch guard rejecting types wider than unsigned long long made that expression fail to compile while preserving the standard integer cases. Overload constraint

Keep object subscripts from silently truncating GNU extended integers through py::int_. Exercise the typed dict wrapper and compile-check the width constraint.

@rwgk rwgk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving, based on manual and codex GPT-6.2-Sol ultra reviews. Only codex caught the edge-case fixed by commit d47415a.

I'm leaving the final review and merging to @henryiii.

@henryiii henryiii self-assigned this Nov 23, 2026
@henryiii
henryiii merged commit 429ee0a into pybind:master Nov 23, 2026
94 checks passed
@henryiii
henryiii deleted the feat/item-accessor-int-key branch November 23, 2026 04:43
@github-actions github-actions Bot added the needs changelog Possibly needs a changelog entry label Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs changelog Possibly needs a changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implicit conversion of ints (and char const*) to handles?

2 participants